var user = {};
var times = new Array();
jQuery(document).ready(function() {

	/* 获取当前用户 */
	getUser();

	$('#bespeakItems').change(function() {
		getNum();
	});
	$('#RECORDDATE').change(function() {
		getNum();
	});
	
	// bindOrg("orgSelect","2","0","");
	bindOrg2("orgSelect");

//	bindItems("bespeakItems", "");
	$('#bespeakItems').select2({
		placeholder : "行政许可项目",
		width: '100%',
		language: 'zh-CN'
	});

	bindDict("recordperiod", "bespeak_period", "");

	initdate();

	// 保存数据事件
	$('#save').click(function() {
		 var ruleData = $("#reserveForm").serializeObject();
		 if (!check(ruleData)){
			 return true;
		 }
		 var b=$("#bespeakItems").val()[0];
		 ruleData.STATE="0";
		 ruleData.USERNAME=user.proposer;
		 ruleData.USERID=user.proposerid;
		 ruleData.ITEMID=b;
		 ruleData.ITEMNAME=$("#bespeakItems").find("option[value="+b+"]").html();
		 ruleData.ORGNAME=$("#orgSelect").val();
		 ruleData.ORGCODE = $("#orgSelect").attr("data");
		 ruleData.COMPANYNAME = user.company;
		 ruleData.COMPANYID = user.companyid;
		 $.ajax({
			 method: 'post',
			 url: basePath() + 'reserve/insert.do',
			 data: JSON.stringify(ruleData),
			 dataType: "json",
			 contentType: "application/json",
			 success: function(data){
				 console.log(data);
				 if(data.result=="1"){
					 $.alert({
						 title: '预约成功！',
						 content: data.info,
						 confirm: function(){
							 //window.location.href="../reserve/index.html";
							 parent.parent.$('#reserveModal').modal('hide');
						 }
					 });
					 parent.$('#bookingTable').bootstrapTable(('refresh'));
				 }else{
					 $.alert(data.info);
				 }
			 }
		 });
	});

	$('#nosave').click(function() {
		parent.$('#reserveModal').modal('hide');
	});
	
	
});

function getUser() {
	$.ajax({
		type : 'POST',
		url : '../../user/getUser.do',
		contentType : 'application/json',
		data : JSON.stringify({}),
		dataType : 'json',
		success : function(data) {
			user = data;
			$("#userName").val(user.proposer);
			$("#contacts").val(user.proposer);
			$("#phoneNum").val(user.mobilephone);
			$("#contactAddr").val(user.contactaddr);
			$("#company").val(user.company);
		}
	})
}

function check(data) {
	var result = true;
	var mustStr = "ITEMID,ORGCODE,LIYOU,USERNAME,CONTACTS,PHONENUM,RECORDDATE,RECORDPERIOD";
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for (var i = 0; i < mustStrs.length; i++) {
		var obj = $('[name="' + mustStrs[i] + '"]');
		var obj1 = obj.attr("placeholder");
		if (obj.val() == "" || obj.val() == null) {
			strMsg += obj1.substring(0, 3)
					+ "<span style='color:red;'><strong>"
					+ obj1.substring(3, obj1.lengh) + "</strong></span>！<br />";
			result = false;
		}
	}
	if (strMsg != "") {
		$.alert(strMsg);
	} else {
		var length = $("#phoneNum").val().length;
		if (length == 11
				&& /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(14[0-9]{1})|)+\d{8})$/
						.test($("#phoneNum").val())) {
			result = true;
		} else {
			result = false;
			strMsg = "请输入正确的手机号!";
			$.alert(strMsg);
		}
	}
	return result;
}
// ajax获取机构数据
function bindOrg(selectID, orgLevel, orgType, selectdata) {
	var infoUrl = projectPath + "share/getOrg1.do";
	$.ajax({
		url : infoUrl,
		data : {
			"orgLevel" : orgLevel,
			"orgType" : orgType
		},
		dataType : "json",
		success : function(data) {
			if (data.result == 1) {
				var dataStr = data.datas.datas;
				console.log(data);
				// 先清空控件数据
				$('#' + selectID).empty();

				var option = $("<option>").val("").text("");
				$('#' + selectID).append(option);

				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].orgCode;
					var text = dataStr[i].orgName;
					var option = $("<option>").val(value).text(text);
					$('#' + selectID).append(option);
				}
				// 选中下拉列表的值
				if (selectdata != "") {
					$('#' + selectID).val(selectdata);
				}
			}
		}
	});
}

// ajax获取可预约项目数据
function bindItems(selectID, selectdata) {
	var infoUrl = projectPath + "reserve/items.do";
	$.ajax({
		url : infoUrl,
		method: 'post',
		dataType : "json",
		contentType : "application/json",
		data : JSON.stringify({
			orgcode : $("#orgSelect").attr("data")
		}),
		success : function(data) {
			// 先清空控件数据
			$('#' + selectID).empty();
			if (data.result == 1) {
				var dataStr = data.datas;

				var option = $("<option>").val("").text("");
				$('#' + selectID).append(option);

				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemid;
					var text = dataStr[i].itemname;
					var option = $("<option>").val(value).text(text);
					$('#' + selectID).append(option);
				}
				// 选中下拉列表的值
				if (selectdata != "") {
					$('#' + selectID).val(selectdata);
				}
			}
		}
	});
}

// ajax获取字典数据
function bindDict(selectID, dictCode, selectdata) {
	var infoUrl = projectPath + "share/getDictDetail.do";
	$.ajax({
		url : infoUrl,
		data : {
			dictcode : dictCode
		},
		async : false,
		dataType : "json",
		success : function(data) {
			if (data.result == 1) {
				var dataStr = data.datas;
				for (var i = 0; i < dataStr.length; i++) {
					times[i] = dataStr[i].itemname;
				}
				// 先清空控件数据
				$('#' + selectID).empty();

				var option = $("<option>").val("").text("");
				$('#' + selectID).append(option);

				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemname;
					var text = dataStr[i].itemname;
					var option = $("<option>").val(value).text(text);
					$('#' + selectID).append(option);
				}
				// 选中下拉列表的值
				if (selectdata != "") {
					$('#' + selectID).val(selectdata);
				}
			}
		}
	});
}

function initdate() {
	var date = new Date();
	date.setDate(date.getDate() + 1);
	$('#RECORDDATE').attr('min', date.format('yyyy-MM-dd'));
}

function bindOrg1(obj) {
	$.ajax({
		method : 'post',
		url : projectPath + "share/getOrg2.do",
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			var cityPicker = new IIInsomniaCityPicker({
				data : data,
				target : '#' + obj,
				callback : function(id) {
					var id = $('#' + obj).attr("data");
					var name = $('#' + obj).val();
					bindItems("bespeakItems", "");
				    getNum();
				}
			});

			cityPicker.init();
		}
	});
}

function bindOrg2(obj){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			//常用海事机构
			var historyjson = data.hisResult;
			//海事机构
			var orgAlljson = data.result;
			
			var topCode = '000000';
			var orgList = JSON2ARRAY3(orgAlljson, topCode);
			var orgHistoryList = JSON2ARRAY2(historyjson);
			InitList(obj, topCode, orgList, orgHistoryList, "", function(orgcode, orgname){
				bindItems("bespeakItems", "");
			    getNum();
			});
			
		}
	});
}

function getNum() {
	if ($("#bespeakItems").val() != null && $("#bespeakItems").val() != '' && $("#RECORDDATE").val() != null
			&& $("#RECORDDATE").val() != '' && $("#orgSelect").val() != null  && $("#orgSelect").val() != '') {
		var reserve = {
			RECORDDATE : $("#RECORDDATE").val(),
			ORGCODE : $("#orgSelect").attr("data"),
			ITEMID : $("#bespeakItems").val()[0],
			RECORDPERIOD : $("#recordperiod").val()
		};
		var data = {
			reserve : reserve,
			times : times
		};
		$.ajax({
			method : 'post',
			url : projectPath + "reserve/getNum.do",
			data : JSON.stringify(data),
			dataType : "json",
			contentType : "application/json",
			success : function(data) {
				var selectdata = $("#recordperiod").val();
				$('#recordperiod').empty();
				var data = data.datas;
				var dataStr = data.split(",");
				var option = $("<option>").val("").text("");
				$('#recordperiod').append(option);
				for (var i = 0; i < dataStr.length; i++) {
					var value = times[i];
					var text = times[i];
					if (!isNaN(dataStr[i])) {
						if (dataStr[i] > 0) {
							text = times[i];
							var option = $("<option>").val(value).text(text);
							$('#recordperiod').append(option);
						} else if (dataStr[i] == -1) {
							text = times[i] + "  (该时间段没有开通预约)"
						}

					}
				}
				if (selectdata != "") {
					$('#recordperiod').val(selectdata);
				}
			}
		});
	}
}
